package com.wust.footprint;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.wust.R;
import com.wust.baby.picture.GetImagesFromSDCard;
import com.wust.baby.picture.ImageGridActivity;
import com.wust.menu.FirstGroupTab;
import com.wust.menu.ImageBtn;
import com.wust.user.User;
import com.wust.user.UserDao;
import com.wust.util.SqltimeToTime;

public class FirstActivity extends ListActivity {
	private ListView listview=null;
	private User user;
	private UserDao userDao;
	private String user_name;
	private SqltimeToTime timeTransfer;
	private ArrayList<HashMap<String, Object>> listItems;
	private ImageBtn iconButton;
	private boolean isMore = false;// menu菜单翻页控制
	AlertDialog menuDialog;// menu菜单Dialog
	GridView menuGrid;
	View menuView;
	int[] menu_image_array = {
			R.drawable.menu_about,
			R.drawable.visitedlist_icon,
			R.drawable.menu_quit};
	/** 菜单文字 **/
	String[] menu_name_array = {  "关于",  "注销","退出"};
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.one);
		SharedPreferences preferences = getSharedPreferences("userinfo",0);
		 user_name=preferences.getString("user_name", "");
		 // iconButton=(ImageBtn) findViewById(R.id.btn1);
		// iconButton.setTextViewName("java");
		 menuView = View.inflate(this, R.layout.gridview_menu, null);
			// 创建AlertDialog
			menuDialog = new AlertDialog.Builder(this.getParent()).create();
			menuDialog.setView(menuView);
			menuDialog.setOnKeyListener(new OnKeyListener() {
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键
						dialog.dismiss();
					return false;
				}
			});

			menuGrid = (GridView) menuView.findViewById(R.id.gridview);
			menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
			/** 监听menu选项 **/
			menuGrid.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					switch (arg2) {
					case 0 :menuDialog.dismiss();
						Intent intent2=new Intent(FirstActivity.this,aboutActivity.class);
					startActivity(intent2);
						break;
					case 1 :menuDialog.dismiss();
					Intent intent=new Intent(FirstActivity.this,LoginActivity.class);
					startActivity(intent);
						break;
					case 2 :finish();
						break;
					}	
				}
			});
	
		listview=getListView();
		listview.setAdapter(buildAdapter());
		timeTransfer=new SqltimeToTime();
	}
	private void showdialog(){
		new AlertDialog.Builder(this).setIcon(R.drawable.menu_quit)
		.setTitle("确定要退出么？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				finish();
			}
		}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				dialog.dismiss();
			}
		}).show();
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		
		super.onListItemClick(l, v, position, id);
		//要跳转的Activity  
		switch((int)id){	
		case 0:jumptoActivity(GetImagesFromSDCard.class, "GetImagesFromSDCard",null,null);
				break;
		case 1:
			jumptoActivity(diaryList.class, "diaryList",null,null);
			break;
		case 2:
			Intent intent = new Intent(
					MediaStore.ACTION_IMAGE_CAPTURE);
			//下面这句指定调用相机拍照后的照片存储的路径
			timeTransfer=timeTransfer.dateToSqlTime(new Date());
			String name="IMG_"+timeTransfer.getYear()+timeTransfer.getMonth()+timeTransfer.getDay()+
					"_"+timeTransfer.getHour()+timeTransfer.getMinute()+timeTransfer.getSecond()+".jpg";
			System.out.println("name:"+name);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
					.fromFile(new File(Environment.getExternalStorageDirectory()+"/image",name)));
			startActivityForResult(intent, 1);
			break;
		}	
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		// 如果是直接从相册获取
		case 1:
			
			break;
		// 如果是调用相机拍照时
		
		default:
			break;

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void jumptoActivity(Class activity,String activityname ,String key[],String value[]){
		Intent intent = new Intent(FirstActivity.this, activity);  
		if(key!=null&&value!=null&&key.length==value.length)
		
			for(int i=0;i<key.length;++i)
				intent.putExtra(key[i], value[i]);
//			intent.putExtra("user_id",user_id);
		
	
        // 把Activity转换成一个Window，然后转换成View  
        Window w = FirstGroupTab.group.getLocalActivityManager()  
                .startActivity(activityname,intent);  
        View view = w.getDecorView();  
        //设置要跳转的Activity显示为本ActivityGroup的内容  
        FirstGroupTab.group.setContentView(view); 
	}
	private  SimpleAdapter buildAdapter(){
		listItems = new ArrayList<HashMap<String, Object>>();    	
        HashMap<String, Object> map1 = new HashMap<String, Object>();           
        map1.put("imageview", R.drawable.video);//图片   
        map1.put("title", "宝贝相册");     //文字   
        listItems.add(map1); 
        HashMap<String, Object> map2 = new HashMap<String, Object>();          
        map2.put("imageview", R.drawable.note);//图片    
        map2.put("title", "心情随笔");     //文字 
        listItems.add(map2);
        HashMap<String, Object> map3 = new HashMap<String, Object>();        
        map3.put("imageview", R.drawable.photo);//图片   
        map3.put("title", "宝贝拍拍");     //文字 
        listItems.add(map3);
           
        //生成适配器的Item和动态数组对应的元素       
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,//数据源        
            R.layout.first_list,//ListItem的XML布局实现       
            //动态数组与ImageItem对应的子项               
            new String[] {"imageview", "title"},        
            //ImageItem的XML文件里面的一个ImageView,两个TextView ID       
            new int[] {R.id.imageview, R.id.title}       
        ); 
        return listItemAdapter;
	}
	private void buildList(){
		listview.setAdapter(buildAdapter());
	}
	private SimpleAdapter getMenuAdapter(String[] menuNameArray,
			int[] imageResourceArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
				R.layout.item_menu, new String[] { "itemImage", "itemText" },
				new int[] { R.id.item_image, R.id.item_text });
		return simperAdapter;
	}
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (menuDialog == null) {
			menuDialog = new AlertDialog.Builder(this).setView(menuView).show();
		} else {
			menuDialog.show();
		}
		return false;// 返回为true 则显示系统menu
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");// 必须创建一项
		return super.onCreateOptionsMenu(menu);
	}
}
